<template>
  <div class="layout">
    <el-container>
      <el-header class="header">
        <div class="logo">EasyBlog</div>
        <div class="user-info">
          <span>欢迎回来，</span>
          <el-dropdown>
            <span class="nick-name">
              {{userInfo.nickName}}
              <span class="iconfont icon-open"></span>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item>个人信息</el-dropdown-item>
                <el-dropdown-item>退出</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
          <img :src="userInfo.avatar" alt="">
        </div>
      </el-header>
      <el-container class="container">
        <el-aside width="200px"
                  class="left-aside">
          <div>
            <el-button class="post-btn">发布</el-button>
          </div>
          <div class="menu-panel">
            <ul>
              <li v-for="(menu,index) in menuList">
                <span class="menu-title-p"
                      @click="openClose(index)">
                  <span :class="['iconfont',menu.icon]"></span>
                  <span class="menu-title">{{menu.title}}</span>
                  <span :class="['iconfont','open-close',menu.open?'icon-open':'icon-close']"></span>
                </span>
                <ul class="sub-menu" 
                    v-show="menu.open">
                  <li v-for="subMenu in menu.children">
                    <router-link :to="subMenu.path"
                                 :class="['sub-menu-item',activePath==subMenu.path?'active':'']">{{subMenu.title}}</router-link>
                  </li>
                </ul>
              </li>
            </ul>
          </div>

        </el-aside>
        <el-main class="right-main">
          <router-view/>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>


<script setup>
import VueCookies from 'vue-cookies'
import { getCurrentInstance, ref, watch } from "vue"
import {useRouter,useRoute} from "vue-router"
const router = useRouter();
const route = useRoute();
const {proxy} = getCurrentInstance();
const menuList = ref([
  {
    title:"博客",
    icon:"icon-xieboke",
    open:true,
    children:[
      {
        title:"博客管理",
        path:"/blog/list",
      },
      {
        title:"分类管理",
        path:"/blog/category",
      }
    ],
  },
  {
    title:"专题",
    icon:"icon-zhuanti",
    open:true,
    children:[
      {
        title:"专题管理",
        path:"/special/special",
      }
    ],
  },
  {
    title:"设置",
    icon:"icon-shezhi",
    open:true,
    children:[
      {
        title:"个人设置",
        path:"/setting/my",
      },
      {
        title:"博客成员",
        path:"/setting/user",
      },
      {
        title:"系统设置",
        path:"/setting/sysSetting",
      }
    ],
  },
  {
    title:"回收站",
    icon:"icon-huishouzhan",
    open:true,
    children:[
      {
        title:"回收站",
        path:"/recover/list",
      }
    ]
    }
])
const openClose = (index) =>{
  const open = menuList.value[index].open;
  menuList.value[index].open = !open
}
const userInfo =ref({});

const init = () => {
  userInfo.value = VueCookies.get("userInfo");
  userInfo.value.avatar = proxy.globalInfo.imageUrl + userInfo.value.avatar;
  console.log(userInfo.value);
}
init();
const activePath = ref(null);
watch(route,(newVal,oldVal) => {
  activePath.value = newVal.path;
},{ immediate: true, deep: true});
</script>

<style lang="scss">
.layout{
  .header{
    border-bottom:1px solid #ddd;
    display: flex;
    align-items: center;
    justify-content: space-between;
    .logo{
      font-size: 30px;
    }
    .user-info{
      display: flex;
      align-items: center;
      .nick-name{
        cursor: pointer;
        color: blue;
      .iconfont{
        font-size: 14px;
        }
      }
      .avatar{
        margin-left: 10px;
        width: 50px;
        img{
          border-radius: 25px;
          width: 100%;
        }
      }
    }
  }
  .container{
    padding-top: 10px;
    background: #f5f6f7;
    height: calc(100hv - 60px);
    .left-aside{
      padding: 0 15px;
      width: 250px;
      .post-btn{
        background: green;
        color: #fff;
        height: 40px;
        width: 100%;
      }
      .menu-panel{
        ul,
        li{
          padding: 0;
          margin: 0;
          list-style: none;
        }
      }
      .menu-title-p{
        line-height: 40px;
        cursor: pointer;
        display: flex;
        .iconfont{
          font-size: 18px;
          color: #91949a;
        }
        .menu-title{
          flex: 1;
          margin-left: 10px;
          color: #3f4042;
        }
        .open-close{
          width: 20px;
        }
      }
      .menu-title-p:hover{
        background: #ddd;
      }
      .sub-menu{
        font-size: 14px;
        .sub-menu-item{
          display: block;
          line-height: 30px;
          padding: 0 28px;
          line-height: 40px;
          text-decoration: none;
          color: #3f4042;
        }
        .active{
          background: #ddd;
        }
        .sub-menu-item:hover{
          background: #ddd;
        }
      }
    }
    .right-main{
      position:relative;
      background: #fff;
      padding: 20px 10px 10px 10px;
    }
    
  }
}
</style>